package TTEST;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import ch.ethz.ssh2.Session;

public class _TerminalDialog //extends JTabbedPane
{
	private static final long serialVersionUID = 1L;

	JPanel botPanel;
	JButton logoffButton;
	JTextArea terminalArea;

	Session sess;
	InputStream in;
	/*static*/ OutputStream out;

	int x, y;
	class RemoteConsumer extends Thread
	{
		char[][] lines = new char[y][];
		int posy = 0;
		int posx = 0;

		private void addText(byte[] data, int len)
		{
			boolean backspace=false;
			String h="";
			for (int i = 0; i < len; i++)
			{
				char c = (char) (data[i] & 0xff);
				//System.out.println("c="+c+"<");
				if (c == 8) // Backspace, VERASE
				{
					System.out.println("TUTAJ");
					if(!backspace )
						System.out.println("znaczek");
					if( !(_window.commandPutFromTextArea== "")){
						System.out.println("PUSTE");
						//aa.setText(aa.getText().substring(0,aa.getText().length()));
					}
					backspace=true;
					continue;
				}
				if(!backspace)
					h+=c;
				if (c == '\r')
				{
					posx = 0;
					continue;
				}
				if (c == '\n')
				{
					setContent(h,aa);
					h="";
					posy++;//h="";
					continue;
				}
				if( h.contains("~]$"))
				{
					_window.koniecpolecenia=true;
				}
				if(c=='$'){
					setContent(h,aa);h="";continue;
				}
				if (c < 32)
				{
					continue;
				}
				continue;
			}
			setContent(h,aa);
		}
		 String content;
		JTextArea aa;
		public RemoteConsumer(JTextArea pp)
		{
			aa=pp;
			//aa.setPreferredSize(new Dimension(50,50));
		}
		public void run()
		{
			byte[] buff = new byte[8192];
			try
			{
				while (true)
				{
					int len = in.read(buff);
					if (len == -1)
						return;
					addText(buff, len);
				}
			}
			catch (Exception e)
			{
			}
		}
	}
	LabWindow _window;
	private String commandtymp="";
	public String getCommand(){ return commandtymp; }
	public _TerminalDialog(LabWindow reference,JTextArea cc, String title, Session sess, int x, int y) throws IOException
	{
		_window=reference;
		terminalArea=cc;
		this.sess = sess;
		in = sess.getStdout();
		out = sess.getStdin();
		this.x = x;
		this.y = y;
		Font f = new Font("Monospaced", Font.PLAIN, 12);
		terminalArea.setFont(f);
		terminalArea.setBackground(Color.BLACK);
		terminalArea.setForeground(Color.ORANGE);
		terminalArea.setCaretColor(Color.BLACK);
		KeyAdapter kl = new KeyAdapter()
		{
			public String hh(){
				StringBuilder sb=new  StringBuilder("");
				for (int i = 0; i < 5; i++) 
					sb.append("     ");
				return sb.toString();
			}
			@Override
			public void keyReleased(KeyEvent e) {
				System.out.println("keyReleased");
			}
			
			@Override
			public void keyPressed(KeyEvent e) {
				System.out.println("keyPressed");
				int charFromKeyboard = e.getKeyChar();
				if ( charFromKeyboard==8 )							//BACKSPACE
				{
					String a=terminalArea.getText().substring(
							terminalArea.getText().length()-1, terminalArea.getText().length()
							).toString();
					if(a.equals("$") || a.equals("#")){
						_window.commandPutFromTextArea="";
						terminalArea.setText(terminalArea.getText()+" ");
					}
					if ( _window.commandPutFromTextArea.length()>0 )
					{
					_window.commandPutFromTextArea=_window.commandPutFromTextArea.substring(0,_window.commandPutFromTextArea.length()-1);
					_window.setTitle(_window.host + hh()+_window.commandPutFromTextArea);
					_window.changeVisibilityButtonresponsibleToSaveInsertedCommand(false);
					}
				}
			}
			public void keyTyped(KeyEvent e)
			{
				int charFromKeyboard = e.getKeyChar();
				if ( charFromKeyboard==8 )							//BACKSPACE
				{
					if ( _window.commandPutFromTextArea.length()>0 )
					{
					_window.commandPutFromTextArea=_window.commandPutFromTextArea.substring(0,_window.commandPutFromTextArea.length()-1);
					_window.setTitle(_window.host + hh()+_window.commandPutFromTextArea);
					_window.changeVisibilityButtonresponsibleToSaveInsertedCommand(false);
					}
				}
				else if( charFromKeyboard!=10 ) {						//another than ENTER
					_window.commandPutFromTextArea+=e.getKeyChar();
					if ( _window.commandPutFromTextArea!= null || _window.commandPutFromTextArea!= "" )
					{
						_window.changeVisibilityButtonresponsibleToSaveInsertedCommand(true);
					}
					try
					{
						_window.setTitle(_window.host + hh()+_window.commandPutFromTextArea);
						out.write(charFromKeyboard);
					}
					catch (IOException e1)
					{
					}
				}
				else {
					_window.changeVisibilityButtonresponsibleToSaveInsertedCommand(false);
					_window.commandPutFromTextArea="";
					_window.setTitle(_window.host);
					_window.executeCommandInConsole(_window.commandPutFromTextArea);
				}
				e.consume();
			}
		};
		terminalArea.addKeyListener(kl);
		rc=new RemoteConsumer(terminalArea);rc.start();
	}
	RemoteConsumer rc;
	String lab_ip;
	public void setContent(String lines,JTextArea pole)
	{
		pole.append(lines);
		//autoscroll
		pole.setCaretPosition(pole.getDocument().getLength());

		//pole.setText(lines);
		SaveOutputIntoTxtFile(lines);
		//terminalArea1.setText(lines);
	}
	void SaveOutputIntoTxtFile(String line){
		Logi.dodawanie(_window.directory,line,_window.shortName/*.host*/);
	}
}
